<!DOCTYPE html >
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport"
          content="width=device-width, initial-scale=1,minimum-scale=1, maximum-scale=1, user-scalable=no">
    <title>大转盘活动</title>

    <link href="index.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="css/main.css">
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/awardRotate.js"></script>


    <!--
        <style>
            * {
                margin: 0;
                padding: 0;
                border: 0;
            }

            .item {
                height: 74vw;
                width: 74vw;
                background: radial-gradient(#FFF 0%, #fff5b2 30%, #ffb2469e 70%, #ffb246 88%);
                box-shadow: 0 0 30px #e56e39;
                border-radius: 50%;
                position: relative;
                top: 2vw;
                left: 2vw;
            }

            html, body {
                height: 100%;
            }

            .bg {
                background: url(images/bg.png) no-repeat;
                background-size: 100% 100%;
                height: 100%;
            }

            .title {
                height: 45px;
                background: #FFF;
                display: flex;
                justify-content: space-between;
                align-items: center;
            }

            .return {
                display: inline-block;
                background: url("images/return.png") no-repeat center;
                background-size: 71% 71%;
                width: 16px;
                height: 32px;
                margin-left: 20px;
            }

            .banner {
                height: 86vw;
                width: 86vw;
                margin-top: 45%;
                background: #dd4400;
                box-shadow: 0 0 30px #E59B0B;
                border-radius: 50%;
                position: relative;

            }

            .turnplate {
                background: #ffd357;
                border-radius: 50%;
                width: 78vw;
                height: 78vw;
                position: absolute;
                top: 4vw;
                left: 4vw;
                box-shadow: 0 0 7px #000;
            }

            .mack-bg {
                position: absolute;
                bottom: 0;
                top: 45px;
                width: 100%;
                box-sizing: inherit;
            }

            #canvas {
                width: 100%;
                height: 100%;
            }

            .record-btn {
                display: block;
                width: 30%;
                margin: 0 auto;
            }

            .record-btn-img {
                width: 100%;
            }

            .lottery-record-bg {
                position: absolute;
                top: 0;
                bottom: 0;
                width: 100%;
                background: rgba(0, 0, 0, 0.5);
            }

            .lottery-record {
                height: 80vw;
                width: 90vw;
                background: url("images/lottery-record.png") no-repeat center;
                background-size: 100%;
                margin: 0 auto;
                margin-top: 36%;
                box-sizing: inherit;
                padding-bottom: 10px;
                display: flex;
                align-items: flex-end;
            }

            .record-content {
                margin: 8% auto;
                width: 70%;
                height: 63%;
                overflow: hidden;
            }

            .tab-header {
                display: flex;
                margin-bottom: 5px;
            }

            .tab-header div {
                float: left;
                font-size: 3.5vw;
                font-family: "Microsoft YaHei";
                font-weight: bold;
                color: rgb(167, 51, 203);
                line-height: 1.875;
                text-align: left;
            }

            .time {
                width: 50%;
            }

            .end {
                width: 32%;
            }

            .state {
                width: 18%;
            }

            .tab-body {
                font-size: 3vw;
                display: flex;
                line-height: 1.875;
            }

            .winn {
                height: 80vw;
                width: 90vw;
                background: url("images/winning.png") no-repeat center;
                background-size: 100%;
                margin: 0 auto;
                margin-top: 32%;
                box-sizing: inherit;
                padding-bottom: 10px;

            }

            .winn-btn {
                display: block;
                margin: 2% auto;
                width: 27%;
                position: relative;
                z-index: 120;
            }

            .winn-btn img {
                width: 100%;
            }

            .winn-btn p {
                width: 100%;
                height: 100%;
                position: absolute;
                top: 0;
                text-align: center;
                line-height: 2.5;
                font-size: 4vw;
                color: #fff;
                text-shadow: -0.977px 2.837px 5px rgba(0, 0, 0, 0.5);
            }

            .result-draw {
                text-align: center;
                font-size: 3.9vw;
                line-height: 2;
                color: rgb(167, 51, 203);
                text-shadow: -0.977px 0.837px 2px rgba(0, 0, 0, 0.5);
            }
        </style>
    -->
    <style>
        .activity-details {
            text-align: center;
            margin-top: 10%;
            font-weight: bold;
            font-size: 4vw;
            line-height: 2;
        }
    </style>
</head>
<body style="overflow-x:hidden;">
<div class="bg">
    <p class="title"><span class="return"></span><span style="font-size: 18px">积分</span><span style=" display: inline-block;width: 16px;
            height: 32px;margin-right: 20px"></span></p>
    <div class="activity-details">
        <p style="color: #FFF">本次积分抽奖持续时间为</p>
        <p style="color: #dd4400">2018年10月25日 17:00 至 2018年10月25日 17:00 </p>
        <p style="color: #FFF;font-size: 4.5vw">单次抽奖消耗积分: <span style="color: #dd4400"> 10000</span></p>
    </div>
    <div class="mack-bg">
        <img src="images/1.png" id="shan-img" style="display:none;"/>
        <img src="images/2.png" id="sorry-img" style="display:none;"/>
        <div class="banner">
            <div class="turnplate">
                <div class="item" id="wheelcanvas">
                    <canvas id="canvas" width="422" height="422"></canvas>
                </div>
                <img class="pointer" src="images/pointer.png"/>
                <div></div>
            </div>
        </div>
        <a href="javascript:void(0)" class="record-btn"> <img src="images/record-btn.png" alt="" class="record-btn-img"></a>
        <div class="lottery-record-bg" id="lottery-record" style="display:none">
            <div class="lottery-record">
                <ul class="record-content">
                    <li class="tab-header">
                        <div class="time">抽奖时间</div>
                        <div class="end">抽奖结果</div>
                        <div class="state">状态</div>
                    </li>
                    <li class="tab-body">
                        <div class="time">2018-09-28 17:00</div>
                        <div class="end">手机一部</div>
                        <div class="state">已发货</div>
                    </li>
                    <li class="tab-body">
                        <div class="time">2018-09-28 17:00</div>
                        <div class="end">手机一部</div>
                        <div class="state">已发货</div>
                    </li>
                    <li class="tab-body">
                        <div class="time">2018-09-28 17:00</div>
                        <div class="end">手机一部</div>
                        <div class="state">已发货</div>
                    </li>
                    <li class="tab-body">
                        <div class="time">2018-09-28 17:00</div>
                        <div class="end">手机一部</div>
                        <div class="state">已发货</div>
                    </li>
                    <li class="tab-body">
                        <div class="time">2018-09-28 17:00</div>
                        <div class="end">手机一部</div>
                        <div class="state">已发货</div>
                    </li>
                    <li class="tab-body">
                        <div class="time">2018-09-28 17:00</div>
                        <div class="end">手机一部</div>
                        <div class="state">已发货</div>
                    </li>
                    <li class="tab-body">
                        <div class="time">2018-09-28 17:00</div>
                        <div class="end">手机一部</div>
                        <div class="state">已发货</div>
                    </li>
                    <li class="tab-body">
                        <div class="time">2018-09-28 17:00</div>
                        <div class="end">手机一部</div>
                        <div class="state">已发货</div>
                    </li>
                    <li class="tab-body">
                        <div class="time">2018-09-28 17:00</div>
                        <div class="end">手机一部</div>
                        <div class="state">已发货</div>
                    </li>
                    <li class="tab-body">
                        <div class="time">2018-09-28 17:00</div>
                        <div class="end">手机一部</div>
                        <div class="state">已发货</div>
                    </li>

                </ul>
            </div>
        </div>
        <div class="lottery-record-bg" id='draw' style="display:none">
            <div class="winn">
                <div style="height: 45%"></div>
                <div class="record-content"
                     style="margin: 0 auto; width: 60%;height: 25%;margin-top: 8%;">
                    <p class="result-draw ">
                        恭喜抽到了积分奖品已发放至您的个人账户
                    </p>
                </div>
                <a href="javascript:void(0)" class="winn-btn" id="confirm" style="display: none">
                    <img src="images/button.png" alt="">
                    <p>确定</p>
                </a>
                <a href="./from/index.html" class="winn-btn" id="toChoose" style="display: none">
                    <img src="images/button.png" alt="">
                    <p>去选择</p>
                </a>
            </div>

        </div>
        <div class="lottery-record-bg" id="thanks" style="display:none">

            <div class="thanks">
                <div style="height: 45%"></div>
                <div class="record-content"
                     style="margin: 0 auto; width: 60%;height: 25%;margin-top: 3%;">
                    <p class="result-thank ">
                        此次抽奖未中奖
                    </p>
                    <p class="result-thank ">
                        祝您下次好运
                    </p>
                </div>
                <a href="javascript:void(0)" class="winn-btn">
                    <img src="images/button.png" alt="">
                    <p>确定</p>
                </a>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    var turnplate = {
        restaraunts: [],				//大转盘奖品名称
        colors: [],					//大转盘奖品区块对应背景颜色
        outsideRadius: 211,			//大转盘外圆的半径
        textRadius: 155,				//大转盘奖品位置距离圆心的距离
        insideRadius: 68,			//大转盘内圆的半径
        startAngle: 0,				//开始角度
        randomRate: [],              //控制获奖率，百分制(相加需等于100%)，对应restaraunts(顺序需要保持一致)，
        bRotate: false				//false:停止;ture:旋转
    };

    $(document).ready(function () {
        //动态添加大转盘的奖品与奖品区域背景颜色
        turnplate.randomRate = ["35%", '35%', '30%', '0%', '0%', '0%', '0%', '0%'];  //小心设置按100%分配
        turnplate.restaraunts = ["10积分", "中性笔一支", "谢谢参与", "15积分", "硬皮本子一个", "20积分", "计算器一个 ", "30积分"];
        turnplate.colors = ["#FFF4D6", "#FFFFFF", "#FFF4D6", "#FFFFFF", "#FFF4D6", "#FFFFFF", "#FFF4D6", "#FFFFFF"];

        var rotateTimeOut = function () {
            $('#wheelcanvas').rotate({
                angle: 0,
                animateTo: 2160,
                duration: 8000,
                callback: function () {
                    alert('网络超时，请检查您的网络设置！');
                }
            });
        };

        //旋转转盘 item:奖品位置; txt：提示语;
        var rotateFn = function (item, txt) {
            var angles = item * (360 / turnplate.restaraunts.length) - (360 / (turnplate.restaraunts.length * 2));
            if (angles < 270) {
                angles = 270 - angles;
            } else {
                angles = 360 - angles + 270;
            }
            $('#wheelcanvas').stopRotate();
            $('#wheelcanvas').rotate({
                angle: 0,
                animateTo: angles + 1800,
                duration: 8000,
                callback: function () {
                    //alert(txt);
                    var reg = RegExp(/积分/);
                    console.log(txt)
                    if (txt === '谢谢参与') {
                        $('#thanks').css({display: 'block'})
                        $('.winn-btn').click(function () {
                            $('#thanks').css({display: 'none'})
                        })
                    } else if (reg.test(txt)) {  //抽到积分
                        $('#confirm').css({display: 'block'})
                        $('#toChoose').css({display: 'none'})

                        console.log('test:' + txt)
                        $('#draw').fadeIn()
                        $('.result-draw').text('恭喜抽到了积分奖品' + txt + '已发放至您的个人账户')
                        $('.winn-btn').click(function () {
                            $('#draw').css({display: 'none'})
                        })
                    } else {  //抽到实体奖品
                        $('#confirm').css({display: 'none'})
                        $('#toChoose').css({display: 'block'})
                        $('#draw').css({display: 'block'})
                        $('.result-draw').text('恭喜抽到了实物奖品' + txt + '请选择您的收货地址')
                        $('.winn-btn').click(function () {
                            $('#draw').css({display: 'none'})
                        })
                    }
                    turnplate.bRotate = !turnplate.bRotate;
                }
            });
        };

        function shark() {
            if (turnplate.bRotate) return;
            turnplate.bRotate = !turnplate.bRotate;
            //获取随机数(奖品个数范围内)
            var item = rnd(turnplate.randomRate);
            //奖品数量等于10,指针落在对应奖品区域的中心角度[252, 216, 180, 144, 108, 72, 36, 360, 324, 288]
            rotateFn(item, turnplate.restaraunts[item - 1]);
        }

        $('.pointer').click(function () {
            shark()
        });

        function rnd(rate) {
            var random = Math.floor(Math.random() * 100);
            var myRandom = [];
            var randomList = [];
            var randomParent = [];
            for (var i = 0; i < 100; i++) {
                myRandom.push(parseInt([i]) + 1);
            }
            for (var i = 0; i < rate.length; i++) {
                var temp = [];
                var start = 0;
                var end = 0;
                randomList.push(parseInt(rate[i].split('%')[0]));
                for (var j = 0; j < randomList.length; j++) {
                    start += randomList[j - 1] || 0
                    end += randomList[j]
                }
                temp = myRandom.slice(start, end);
                randomParent.push(temp)

            }
            console.log(randomParent)
            for (var i = 0; i < randomParent.length; i++) {
                console.log(randomParent)
                if ($.inArray(random, randomParent[i]) >= 0) {
                    return (i + 1)
                }
            }

            console.log(myRandom, randomList, randomParent)

        }
    });


    //页面所有元素加载完毕后执行drawRouletteWheel()方法对转盘进行渲染
    window.onload = function () {
        drawRouletteWheel();
    };

    function drawRouletteWheel() {

        var canvas = document.getElementById("canvas");
        if (canvas.getContext) {
            //根据奖品个数计算圆周角度
            var arc = Math.PI / (turnplate.restaraunts.length / 2);
            var ctx = canvas.getContext("2d");
            //在给定矩形内清空一个矩形
            ctx.clearRect(0, 0, 422, 422);
            //strokeStyle 属性设置或返回用于笔触的颜色、渐变或模式
            ctx.strokeStyle = "#f48819";
            //font 属性设置或返回画布上文本内容的当前字体属性

            ctx.arc(211, 211, 211, 0, Math.PI * 2, false);
            //绘制渐变外框
            var grd = ctx.createRadialGradient(211, 211, 5, 211, 211, 211);
            grd.addColorStop(0, 'white');
            grd.addColorStop(1, '#ffb246');
            //使用经向渐变填充
            ctx.fillStyle = grd;
            ctx.fill()
            /*ctx.fillRect(0, 0, canvas.width, canvas.height);*/
            /**/
            ctx.font = '16px Microsoft YaHei';
            for (var i = 0; i < turnplate.restaraunts.length; i++) {
                var angle = turnplate.startAngle + i * arc;
                ctx.fillStyle = 'transparent';
                ctx.beginPath();
                //arc(x,y,r,起始角,结束角,绘制方向) 方法创建弧/曲线（用于创建圆或部分圆）
                ctx.arc(211, 211, turnplate.outsideRadius, angle, angle + arc, false);
                ctx.arc(211, 211, turnplate.insideRadius, angle + arc, angle, true);
                ctx.stroke();
                ctx.fill();
                //锁画布(为了保存之前的画布状态)
                ctx.save();

                //----绘制奖品开始----
                ctx.fillStyle = "#E5302F";
                var text = turnplate.restaraunts[i];
                var line_height = 17;
                //translate方法重新映射画布上的 (0,0) 位置
                ctx.translate(211 + Math.cos(angle + arc / 2) * turnplate.textRadius, 211 + Math.sin(angle + arc / 2) * turnplate.textRadius);

                //rotate方法旋转当前的绘图
                ctx.rotate(angle + arc / 2 + Math.PI / 2);

                /** 下面代码根据奖品类型、奖品名称长度渲染不同效果，如字体、颜色、图片效果。(具体根据实际情况改变) **/
                if (text.indexOf("M") > 0) {//流量包
                    var texts = text.split("M");
                    for (var j = 0; j < texts.length; j++) {
                        ctx.font = j == 0 ? 'bold 20px Microsoft YaHei' : '16px Microsoft YaHei';
                        if (j == 0) {
                            ctx.fillText(texts[j] + "M", -ctx.measureText(texts[j] + "M").width / 2, j * line_height);
                        } else {
                            ctx.fillText(texts[j], -ctx.measureText(texts[j]).width / 2, j * line_height);
                        }
                    }
                } else if (text.indexOf("M") == -1 && text.length > 6) {//奖品名称长度超过一定范围
                    text = text.substring(0, 6) + "||" + text.substring(6);
                    var texts = text.split("||");
                    for (var j = 0; j < texts.length; j++) {
                        ctx.fillText(texts[j], -ctx.measureText(texts[j]).width / 2, j * line_height);
                    }
                } else {
                    //在画布上绘制填色的文本。文本的默认颜色是黑色
                    //measureText()方法返回包含一个对象，该对象包含以像素计的指定字体宽度
                    ctx.fillText(text, -ctx.measureText(text).width / 2, 0);
                }

                //添加对应图标
                if (text.indexOf("积分") > 0) {
                    var img = document.getElementById("shan-img");
                    img.onload = function () {
                        ctx.drawImage(img, -15, 10);
                    };
                    ctx.drawImage(img, -15, 10);
                } else if (text.indexOf("谢谢参与") >= 0) {
                    var img = document.getElementById("sorry-img");
                    img.onload = function () {
                        ctx.drawImage(img, -15, 10);
                    };
                    ctx.drawImage(img, -15, 10);
                }
                //把当前画布返回（调整）到上一个save()状态之前
                ctx.restore();
                //----绘制奖品结束----
            }
        }
    }

</script>
<script>
    $(function () {
        drawRouletteWheel();
        $('.record-btn').click(function () {
            $('#lottery-record').css({display: 'block'})
        })
        $('#lottery-record').click(function () {
            $(this).css({display: 'none'})
        }).children().click(function (e) {
            e.stopPropagation();
        })
    })
</script>
</body>

</html>